package com.ls.x.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ls.x.activity.beans.AlertModelBean;
import com.ls.x.tool.DatabaseHelper;

public class AlertModelListViewAdapter extends BaseAdapter {
	private int textViewResourceId = 0;
	private Context context;

	private ArrayList<AlertModelBean> alertModelBeanList = new ArrayList<AlertModelBean>();
	private Map<Integer, Integer> idAndPositionMap = new HashMap<Integer, Integer>();

	private void init() {

		DatabaseHelper databaseHelper = DatabaseHelper.getDBhelper(context);

		SQLiteDatabase db = databaseHelper.getReadableDatabase();

		Cursor cursor = db.rawQuery("select * from remind_mode", null);
		int i = 0;
		while (cursor.moveToNext()) {

			AlertModelBean alertModelBean = new AlertModelBean();

			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String mode = cursor.getString(2);
			String remindTimes = cursor.getString(3);
			String smsNumber = cursor.getString(4);
			String alertContent = cursor.getString(5);

			alertModelBean.setId(id);
			alertModelBean.setName(name);
			alertModelBean.setMode(mode);
			alertModelBean.setRemindTimes(remindTimes);
			alertModelBean.setSmsNumber(smsNumber);
			alertModelBean.setAlertContent(alertContent);

			alertModelBeanList.add(alertModelBean);

			idAndPositionMap.put(id, i);
			i++;
		}

		db.close();
		databaseHelper.close();

	}

	public AlertModelListViewAdapter(Context context, int textViewResourceId) {
		this.context = context;
		this.textViewResourceId = textViewResourceId;
		init();
	}

	public void setTextViewResourceId(int textViewResourceId) {
		this.textViewResourceId = textViewResourceId;
	}

	@Override
	public int getCount() {

		return alertModelBeanList.size();
	}

	@Override
	public Object getItem(int position) {
		return alertModelBeanList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		TextView text = null;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = inflater.inflate(textViewResourceId, null);
			view.findViewById(textViewResourceId);
			text = (TextView) view;

		} else {
			text = (TextView) convertView;
		}

		text.setText(((AlertModelBean) getItem(position)).getName());

		// 设置各行换色
		if (position % 2 == 0) {
			text.setBackgroundColor(Color.parseColor("#CBCBCB"));
		} else {
			text.setBackgroundColor(Color.parseColor("#E7E7E7"));
		}
		return text;
	}

	public void refresh() {
		alertModelBeanList.clear();
		init();
		notifyDataSetChanged();
	}

	public int getPositionByRemindModelId(int remindModelId) {
		return idAndPositionMap.get(remindModelId);
	}
}
